/*
 * ======== Standard MSP430 includes ========
 */
#include <msp430.h>

/*
 * ======== Grace related includes ========
 */
#include <ti/mcu/msp430/csl/CSL.h>

/*
 *  ======== main ========
 */
/*
 * MOTOR HW DEFS
 */ 
 //P3.1 connected to M1 in2
#define M1_PWM_100 51 
#define M1_FW P3OUT |=  0x02; TBCCR2 = M1_PWM_100 - TBCCR2 
#define M1_BW P3OUT &= ~0x02; TBCCR2 = M1_PWM_100 - TBCCR2
#define M1_ST P3OUT &= ~0x02; TBCCR2 = 0
 
long g_ldivider = 0; 

typedef enum{
	E_IDLE,
	E_LED,
	E_PWM
}EMSTATE;

typedef struct{
	EMSTATE state;
	unsigned char rx_buf;
	unsigned char tx_avaliabe;
	unsigned char tx_buf;
}SMACHINE;
	
SMACHINE g_sRX_machine; 
 
int main(int argc, char *argv[])
{
    CSL_init();                     // Activate Grace-generated configuration
    __enable_interrupt();           // Set global interrupt enable
    
    // >>>>> Fill-in user code here <<<<<
    P1OUT |= 0x01;
    P1OUT |= 0x02;
    M1_ST;
    //M1_FW;
    //TBCCR2 = 30;
    
    _BIS_SR(LPM0_bits + GIE);
}

void RX_Machine_execute(void)
{
	switch(g_sRX_machine.state)
	{
		case E_IDLE:
			if((g_sRX_machine.rx_buf == 'l') || (g_sRX_machine.rx_buf == 'L'))
			{
				g_sRX_machine.rx_buf = 0;
				g_sRX_machine.state = E_LED;
			}
			else if((g_sRX_machine.rx_buf == 'p') || (g_sRX_machine.rx_buf == 'P'))
			{
				g_sRX_machine.rx_buf = 0;
				g_sRX_machine.state = E_PWM;
			}
			else if(g_sRX_machine.rx_buf == '+')
			{
				g_sRX_machine.rx_buf = 0;
				TBCCR2++;
			}
			else if(g_sRX_machine.rx_buf == '-')
			{
				g_sRX_machine.rx_buf = 0;
				TBCCR2--;
			}
			else if(g_sRX_machine.rx_buf == ',')
			{
				g_sRX_machine.rx_buf = 0;
				M1_FW;
			}
			else if(g_sRX_machine.rx_buf == '.')
			{
				g_sRX_machine.rx_buf = 0;
				M1_BW;
			}
			else if(g_sRX_machine.rx_buf == '*')
			{
				g_sRX_machine.rx_buf = 0;
				M1_ST;
			}
		break;	
		case E_LED:
			P1OUT ^= 0x02;
			g_sRX_machine.tx_buf = P1OUT;
			g_sRX_machine.tx_avaliabe = 0xAC;
			g_sRX_machine.state = E_IDLE;
		break;
		case E_PWM:
			switch(g_sRX_machine.rx_buf)
			{
				case '1': TBCCR2 = 0;   		g_sRX_machine.rx_buf = 0; g_sRX_machine.state = E_IDLE; break;
				case '2': TBCCR2 = 25;  		g_sRX_machine.rx_buf = 0; g_sRX_machine.state = E_IDLE; break;
				case '3': TBCCR2 = 45;  		g_sRX_machine.rx_buf = 0; g_sRX_machine.state = E_IDLE; break;
				case '4': TBCCR2 = 55;  		g_sRX_machine.rx_buf = 0; g_sRX_machine.state = E_IDLE; break;
				case '5': TBCCR2 = M1_PWM_100; 	g_sRX_machine.rx_buf = 0; g_sRX_machine.state = E_IDLE; break;
				case '+': TBCCR2++;     		g_sRX_machine.rx_buf = 0; g_sRX_machine.state = E_IDLE; break;
				case '-': TBCCR2--;     		g_sRX_machine.rx_buf = 0; g_sRX_machine.state = E_IDLE; break;
			}
		break;			
		default:
			g_sRX_machine.rx_buf = 0;
			g_sRX_machine.state = E_IDLE;	
	}
}

void Timer_A3_ISR(void)
{
	if(g_ldivider++ > 4)
	{
		g_ldivider = 0;
	}
	
	RX_Machine_execute();
	if(g_sRX_machine.tx_avaliabe == 0xAC)
	{
		UCA0TXBUF = g_sRX_machine.tx_buf;
		g_sRX_machine.tx_avaliabe = 0;
	}
}

void UART_RX_ISR(void)
{
	P1OUT ^= 0x01;
	g_sRX_machine.rx_buf = UCA0RXBUF;
	IFG2 &= ~(UCA0RXIFG);
}



